﻿Imports System.Data.SqlClient
Imports RunEnv
Imports UITool
Public Class dlgUserGroupMap
    Private mstrUserGroupID As String
    Private mdtUserGroupMap As New DataTable
    Private mdtUser As DataTable
    Private mdtAdapter As SqlDataAdapter

    Public Sub ShowOpen(ByVal userGroupID As String)
        Try
            If Len(userGroupID) <= 0 Then Exit Sub
            mstrUserGroupID = userGroupID
            Me.lblUserGroupID.Text = userGroupID
            InitDB()
            InitList()
            Me.ShowDialog()
        Catch ex As Exception
            MainException(ex)
        End Try

    End Sub

    Private Sub InitDB()
        Dim sql As String
        Dim insertCommand As New SqlCommand
        Dim deleteCommand As New SqlCommand
        sql = "Select * From B_UserGroupMap Where UserGroupID ='" & mstrUserGroupID & "'"
        mdtAdapter = New SqlDataAdapter(sql, CRunEnv.DBConnection.sqlConnection)
        mdtAdapter.Fill(mdtUserGroupMap)
        mdtUserGroupMap.PrimaryKey = {mdtUserGroupMap.Columns("UserID"), mdtUserGroupMap.Columns("UserGroupID")}
        insertCommand.Connection = CRunEnv.DBConnection.sqlConnection
        insertCommand.CommandText = "Insert Into B_UserGroupMap (UserID,UserGroupID) Values (@UserID,@UserGroupID)"
        insertCommand.Parameters.Add("@UserID", SqlDbType.NVarChar, 20, "UserID")
        insertCommand.Parameters.Add("@UserGroupID", SqlDbType.NVarChar, 50, "UserGroupID")
        mdtAdapter.InsertCommand = insertCommand

        deleteCommand.Connection = CRunEnv.DBConnection.sqlConnection
        deleteCommand.CommandText = "Delete From B_UserGroupMap Where UserID =@UserID And UserGroupID =@UserGroupID"
        deleteCommand.Parameters.Add("@UserID", SqlDbType.NVarChar, 20, "UserID")
        deleteCommand.Parameters.Add("@UserGroupID", SqlDbType.NVarChar, 50, "UserGroupID")
        mdtAdapter.DeleteCommand = deleteCommand
        sql = "Select B_User.UserID,B_User.UserName From B_User Where B_User.IsStop = 0 "
        mdtUser = CRunEnv.DBConnection.GetDataTable(sql)

    End Sub


    Private Sub InitList()
        Try
            ListView1.Items.Clear()
            For Each dtRow As DataRow In mdtUserGroupMap.Rows
                ListView1.Items.Add(dtRow.Item("UserID"))
            Next
        Catch ex As Exception
            MainException(ex)
        End Try
    End Sub

    Private Sub cmdAdd_Click(sender As System.Object, e As System.EventArgs) Handles cmdAdd.Click
        Dim newUsers As Collection
        Dim dlg As New dlgCommonSelect
        Try
            If mdtUser.Rows.Count <= 0 Then
                MsgBox("不存在用户！", MsgBoxStyle.Information, "系统提示")
                Exit Sub
            End If
            newUsers = dlg.ShowOpen(mdtUser, "添加用户", "UserID")
            If newUsers.Count <= 0 Then Exit Sub

            Dim newRow As DataRow
            For Each newUserID As String In newUsers
                If mdtUserGroupMap.Select("UserID='" & newUserID & "'").Length > 0 Then
                    Continue For
                End If
                newRow = mdtUserGroupMap.NewRow
                newRow.Item("UserID") = newUserID
                newRow.Item("UserGroupID") = mstrUserGroupID
                mdtUserGroupMap.Rows.Add(newRow)
            Next

            If mdtAdapter.Update(mdtUserGroupMap) > 0 Then
                InitList()
            End If
        Catch ex As Exception
            MainException(ex)
        End Try
    End Sub
    Private Sub cmdClose_Click(sender As System.Object, e As System.EventArgs) Handles cmdClose.Click
        Me.Close()
    End Sub
    Private Sub cmdRemove_Click(sender As System.Object, e As System.EventArgs) Handles cmdRemove.Click
        Dim userIDs As DataRow()
        Dim listViewItem1 As Windows.Forms.ListViewItem
        Dim filterStr As String = ""

        Try
            If ListView1.SelectedItems.Count <= 0 Then Exit Sub
            For Each listViewItem1 In ListView1.SelectedItems
                If Len(filterStr) = 0 Then
                    filterStr = "UserID ='" & listViewItem1.Text & "'"
                Else
                    filterStr += " Or UserID ='" & listViewItem1.Text & "'"
                End If
            Next

            userIDs = mdtUserGroupMap.Select(filterStr)

            For Each userID As DataRow In userIDs
                userID.Delete()
            Next

            If mdtAdapter.Update(mdtUserGroupMap) > 0 Then
                InitList()
            End If
        Catch ex As Exception
            MainException(ex)
        End Try
    End Sub
End Class